﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TimKen.Web.User
{
    public partial class MessageSendView : TimKen.App.WebPage
    {
        TimKen.BussLogic.InboxManager inboxM = new TimKen.BussLogic.InboxManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (CurrentUserName == null || CurrentUserName == "")
                {
                    Alert("您必须登录！", "../Index.aspx");
                }
                if (Request.QueryString["MsgID"] != null)
                {
                    MsgID = Request.QueryString["MsgID"];
                    BindMsg();
                }
            }
        }

        protected string MsgID
        {
            get
            {
                if (ViewState["MsgID"] != null)
                    return (ViewState["MsgID"].ToString());
                else
                    return "";
            }
            set
            {
                ViewState["MsgID"] = value;
            }
        }

        protected void BindMsg()
        {
            TimKen.DataEntity.Inbox msg = inboxM.GetModel(int.Parse(MsgID));
            if (msg == null)
            {
                Response.Redirect("/ErrorAuthority.aspx");
            }
            else if (msg.ToID != CurrentUserName && msg.FromID != CurrentUserName)
            {
                Response.Redirect("/ErrorAuthority.aspx");
            }
            else
            {
                if (!msg.IsRead)
                {
                    msg.IsRead = true;
                    inboxM.Update(msg);
                }
                labCreateDate.Text = msg.CreateDate.Value.ToShortDateString();
                labFromID.Text = msg.FromID;
                labToID.Text = msg.ToID;
                labMessage.Text = msg.Message.Replace("\n", "</br>");
                labTitle.Text = msg.Title;
            }
        }

    }
}
